package com.yy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.common.result.PageResult;
import com.yy.common.result.Result;
import com.yy.entity.Hospitalprice;
import com.yy.entity.Pharmacy;
import com.yy.entity.Register;
import com.yy.model.dto.His;
import com.yy.model.dto.ZhongJHDTO;
import com.yy.query.RegisterQuery;
import com.yy.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Author Yang
 * @Create 2024/6/14 9:39
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/drugbilling")
@Tag(name = "住院管理.药品记账")
public class DrugbillingController {
    //入院
    private final ReportService reportService;
    //住院表
    private final RegisterService registerService;
    //科室
    private final DepartmentsService departmentsService;
    //医生
    private final DoctorService doctorService;
    //床位
    private final BedService bedService;
    //预交表
    private final PayService payService;
    //药房表
    private final PharmacyService pharmacyService;
    //住院收费表
    private final HospitalpriceService hospitalpriceService;
    //住院项目收费表
    private final InoutpatienttypeService inoutpatienttypeService;

    @GetMapping("/listDrug")
    @Operation(summary = "药品列表分页查询")
    public PageResult<Pharmacy> listDrug(RegisterQuery registerQuery) {
        IPage<Pharmacy> page = pharmacyService.getPage(registerQuery);
        return PageResult.success(page);
    }

    @GetMapping("/listhosp")
    @Operation(summary = "根据住院号查询已买药品信息")
    public PageResult<Hospitalprice> listhosp(His his) {
        QueryWrapper<Hospitalprice> wrapper = new QueryWrapper<>();
        wrapper.eq("registerid", his.getHospitalizationnumber());
        wrapper.eq("state", 0);
        Page<Hospitalprice> page = new Page<Hospitalprice>(his.getPageNum(), his.getPageSize());
        return PageResult.success(hospitalpriceService.page(page, wrapper));
    }

    @GetMapping("/listphar/{pharmacyid}")
    @Operation(summary = "根据药品id查询药品信息")
    public Result listphar(@PathVariable int pharmacyid) {
        LambdaQueryWrapper<Pharmacy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Pharmacy::getPharmacyid, pharmacyid);
        Pharmacy one = pharmacyService.getOne(wrapper);
        return Result.success(one);
    }

    @PostMapping("/zhongff")
    @Operation(summary = "添加病人药品")
    public Result zhongff(@RequestBody ZhongJHDTO zhongJHDTO) {
        boolean f = registerService.update(new LambdaUpdateWrapper<Register>()
                .eq(Register::getRegisterid, zhongJHDTO.getHospitalizationnumber())
                .set(Register::getPrice, zhongJHDTO.getPrice()));
        boolean d = pharmacyService.update(new LambdaUpdateWrapper<Pharmacy>()
                .eq(Pharmacy::getPharmacyid, zhongJHDTO.getPharmacyid())
                .set(Pharmacy::getDrugstorenum, zhongJHDTO.getQuantitytwo()));
        Hospitalprice hospitalprice = new Hospitalprice();
        BeanUtils.copyProperties(zhongJHDTO, hospitalprice);
        hospitalprice.setRegisterid(zhongJHDTO.getHospitalizationnumber());
        hospitalprice.setDurgname(zhongJHDTO.getPharmacy().getPharmacyname());
        hospitalprice.setDurgnum(zhongJHDTO.getQuantity());
        hospitalprice.setRepiceprice(zhongJHDTO.getPharmacy().getSellingprice());
        hospitalprice.setRepicetotal(zhongJHDTO.getQuantity() * zhongJHDTO.getPharmacy().getSellingprice());
        hospitalprice.setHtime(new Date());
        hospitalprice.setState(0);
        boolean save = hospitalpriceService.save(hospitalprice);
        return Result.success();
    }

    @PostMapping("/updatepricejians")
    @Operation(summary = "删除病人药品")
    public Result updatepricejians(@RequestBody ZhongJHDTO zhongJHDTO) {
        //删除已购买的药品
        int b = hospitalpriceService.deletehosp(zhongJHDTO.getHospitalizationnumber());
        //根据药品名查询药品数量
        LambdaQueryWrapper<Pharmacy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Pharmacy::getPharmacyname, zhongJHDTO.getPharmacyname());
        Pharmacy one = pharmacyService.getOne(wrapper);
        //修改药品数量
        boolean d = pharmacyService.update(new LambdaUpdateWrapper<Pharmacy>()
                .eq(Pharmacy::getPharmacyid, one.getPharmacyid())
                .set(Pharmacy::getDrugstorenum, one.getDrugstorenum() + zhongJHDTO.getQuantity()));
        //根据住院号修改已用金额
        boolean f = registerService.update(new LambdaUpdateWrapper<Register>()
                .eq(Register::getRegisterid, zhongJHDTO.getRegisterid())
                .set(Register::getPrice, zhongJHDTO.getPrice()));
        return Result.success();
    }

}
